package com.yhsoft.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhsoft.common.service.TokenService;
import com.yhsoft.goods.controller.Po.SkuChargeInfoPo;
import com.yhsoft.goods.controller.Po.skuInfoPo;
import com.yhsoft.goods.controller.requset.SetMallAddPriceInVo;
import com.yhsoft.goods.controller.response.GetMallAddPriceOutVo;
import com.yhsoft.goods.entity.CompanyGoods;
import com.yhsoft.goods.entity.CompanySkuCharge;
import com.yhsoft.goods.entity.SkuInfo;
import com.yhsoft.goods.mapper.CompanyGoodsMapper;
import com.yhsoft.goods.mapper.CompanySkuChargeMapper;
import com.yhsoft.goods.mapper.SkuInfoMapper;
import com.yhsoft.goods.service.ICompanySkuChargeService;
import com.yhsoft.goods.service.ISkuInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品sku信息表 服务实现类
 * </p>
 *
 * @author yyyyyyyh
 * @since 2023-09-22
 */
@Service
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoMapper, SkuInfo> implements ISkuInfoService {

    @Resource
    private CompanyGoodsMapper companyGoodsMapper;

    @Resource
    private CompanySkuChargeMapper companySkuChargeMapper;

    @Resource
    private ICompanySkuChargeService iCompanySkuChargeService;

    @Override
    public List<skuInfoPo> getSkuInfoByGoodsId(Integer goodsId) {
        QueryWrapper<skuInfoPo> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id", goodsId);
        List<skuInfoPo> skuInfoPos = this.baseMapper.getSkuInfoListByGoodsId(wrapper);
        return skuInfoPos;
    }

    @Override
    public List<SkuChargeInfoPo> getSkuChargeInfoByGoodsId(Integer goodsId) {
        QueryWrapper<skuInfoPo> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id", goodsId);
        List<SkuChargeInfoPo> skuChargeInfoPos = this.baseMapper.getSkuChargeInfoListByGoodsId(wrapper);
        return skuChargeInfoPos;
    }

    @Override
    public List<GetMallAddPriceOutVo> getMallAddPrice(Integer goodsId) {
        LambdaQueryWrapper<SkuInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SkuInfo::getGoodsId, goodsId);
        List<SkuInfo> skuInfos = this.list(wrapper);

        LambdaQueryWrapper<CompanyGoods> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(CompanyGoods::getGoodsId, goodsId);
        wrapper1.eq(CompanyGoods::getCompanyId, TokenService.getUserBytoken().getCompanyId());
        CompanyGoods companyGoods = companyGoodsMapper.selectOne(wrapper1);
        BigDecimal charge = BigDecimal.ZERO;
        if (companyGoods != null) {
            charge = companyGoods.getCustomCharge() != null ? new BigDecimal(companyGoods.getCustomCharge()).divide(new BigDecimal(100)) : BigDecimal.ZERO;
        }

        LambdaQueryWrapper<CompanySkuCharge> wrapper3 = new LambdaQueryWrapper<>();
        wrapper3.eq(CompanySkuCharge::getGoodsId, goodsId);
        wrapper3.eq(CompanySkuCharge::getCompanyId, TokenService.getUserBytoken().getCompanyId());
        List<CompanySkuCharge> chargeList = companySkuChargeMapper.selectList(wrapper3);
        Map<Integer, BigDecimal> chargeMap = null;
        if (chargeList.size() > 0) {
            chargeMap = chargeList.stream().collect(Collectors.toMap(CompanySkuCharge::getSkuInfoId, CompanySkuCharge::getMallAddPrice));
        }

        List<GetMallAddPriceOutVo> outVos = new ArrayList<>();
        for (SkuInfo skuInfo : skuInfos) {
            GetMallAddPriceOutVo outVo = new GetMallAddPriceOutVo();
            // 拷贝属性
            BeanUtils.copyProperties(skuInfo, outVo);
            BigDecimal jscb = skuInfo.getSkuPrice().multiply(BigDecimal.ONE.add(charge));
            outVo.setJscb(jscb);
            outVo.setSkuInfoId(skuInfo.getId());
            if (chargeMap != null && chargeMap.get(skuInfo.getId()) != null) {
                outVo.setMallAddPrice(chargeMap.get(skuInfo.getId()));
            }
            outVos.add(outVos.size(), outVo);
        }

        return outVos;
    }

    @Override
    public String setMallAddPrice(List<SetMallAddPriceInVo> inVos) {
        Integer companyId = TokenService.getUserBytoken().getCompanyId();

        LambdaQueryWrapper<CompanySkuCharge> wrapper = new LambdaQueryWrapper<>();
        for (SetMallAddPriceInVo inVo : inVos) {
            CompanySkuCharge companySkuCharge = new CompanySkuCharge();
            companySkuCharge.setGoodsId(inVo.getGoodsId());
            companySkuCharge.setSkuInfoId(inVo.getSkuInfoId());
            companySkuCharge.setMallAddPrice(inVo.getMallAddPrice());
            companySkuCharge.setCompanyId(TokenService.getUserBytoken().getCompanyId());
            wrapper.eq(CompanySkuCharge::getCompanyId, companyId);
            wrapper.eq(CompanySkuCharge::getSkuInfoId, inVo.getSkuInfoId());
            iCompanySkuChargeService.saveOrUpdate(companySkuCharge, wrapper);
            wrapper.clear();
        }
        return "success";
    }
}
